package com.opencloud.ywt.provider.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.ywt.client.model.entity.BhyfXhbWatch;
import com.opencloud.ywt.client.model.entity.BhyfXhbWatchInfo;
import com.opencloud.ywt.client.model.entity.BhyfXhbWatchRecord;
import com.opencloud.ywt.provider.service.BhyfWatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 园务通
 * @author yeyong
 */
@RestController
@Api(value = "园务通", tags = "园务通")
public class BhyfWatchController {

    @Autowired
    private BhyfWatchService bhyfWatchService;


    /**
     * 获取教案列表信息
     * @param map
     * @return
     */
    @ApiOperation(value = "获取教案列表信息", notes = "获取教案列表信息")
    @GetMapping("/watch/infolist")
    public ResultBody<IPage<BhyfXhbWatch>> getBhyfXhbWatchListPage(
            @RequestParam(required = false) Map map,String startTime,String endTime
    )
    {
        return ResultBody.ok().data(bhyfWatchService.findListPage(new PageParams(map),startTime,endTime));
    }

    /**
     * 新增教案
     * @param classid
     * @param classname
     * @param datastart
     * @param dataend
     * @param gid
     * @param gradenum
     * @param personid
     * @param studentname
     * @param teacherid
     * @param teachername
     * @param watchstart
     * @param watchend
     * @param watchinfo
     * @param watchallinfo
     * @return
     */
    @ApiOperation(value = "新增教案", notes = "新增教案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classid", value = "班级ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "classname", value = "班级名称", required = true, paramType = "form"),
            @ApiImplicitParam(name = "datastart", value = "数据开始时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "dataend", value = "数据结束时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "gid", value = "园所ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "gradenum", value = "年级ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "personid", value = "学生ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "studentname", value = "学生姓名", required = true, paramType = "form"),
            @ApiImplicitParam(name = "teacherid", value = "老师ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "teachername", value = "老师姓名", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchtype", value = "教案类型", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchstart", value = "关注开始时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchend", value = "关注结束时间", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchinfo", value = "已选关注问题", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchallinfo", value = "所有关注问题", required = true, paramType = "form"),
            @ApiImplicitParam(name = "gname", value = "园所名称", required = false, paramType = "form")
    })
    @PostMapping("/watch/addinfo")
    public ResultBody<BhyfXhbWatch> addBhyfXhbWatch(
            @RequestParam(name = "classid") Integer classid,
            @RequestParam(name = "classname") String classname,
            @RequestParam(name = "datastart") String datastart,
            @RequestParam(name = "dataend") String dataend,
            @RequestParam(name = "gid") Integer gid,
            @RequestParam(name = "gradenum") Integer gradenum,
            @RequestParam(name = "watchtype") Integer watchtype,
            @RequestParam(name = "personid") Long personid,
            @RequestParam(name = "studentname") String studentname,
            @RequestParam(name = "teacherid") Long teacherid,
            @RequestParam(name = "teachername") String teachername,
            @RequestParam(name = "watchstart") String watchstart,
            @RequestParam(name = "watchend",required = false) String watchend,
            @RequestParam(name = "watchinfo") String watchinfo,
            @RequestParam(name = "watchallinfo") String watchallinfo,
            @RequestParam(name = "gname",required = false) String gname
    ) {
        BhyfXhbWatch bhyfXhbWatch = new BhyfXhbWatch();
        bhyfXhbWatch.setClassid(classid);
        bhyfXhbWatch.setClassname(classname);
        bhyfXhbWatch.setCreatetime(new Date());
        bhyfXhbWatch.setDatastart(DateUtil.parse(datastart));
        bhyfXhbWatch.setDataend(DateUtil.parse(dataend));
        bhyfXhbWatch.setEndword("");
        bhyfXhbWatch.setEndstatus(0);
        bhyfXhbWatch.setGid(gid);
        bhyfXhbWatch.setGradenum(gradenum);
        bhyfXhbWatch.setPersonid(personid);
        bhyfXhbWatch.setStudentname(studentname);
        bhyfXhbWatch.setTeacherid(teacherid);
        bhyfXhbWatch.setTeachername(teachername);
        bhyfXhbWatch.setWatchinfo(watchinfo);
        bhyfXhbWatch.setWatchallinfo(watchallinfo);
        bhyfXhbWatch.setWatchstart(DateUtil.parse(watchstart));
        if (watchend != null){
            bhyfXhbWatch.setWatchend(DateUtil.parse(watchend));
        }
        bhyfXhbWatch.setWatchstatus(1);
        bhyfXhbWatch.setWatchtype(watchtype);
        bhyfXhbWatch.setUpdatetime(new Date());
        if(gname!=null) {
            bhyfXhbWatch.setGname(gname);
        }
        PageParams pageParams = new PageParams();
        Map<String, Object> requestMap = Maps.newHashMap();
        requestMap.put("personid",personid);
        requestMap.put("watchtype",watchtype);
        requestMap.put("watchstatus",1);
        pageParams.setRequestMap(requestMap);
        IPage<BhyfXhbWatch> xhbWatchIPage = bhyfWatchService.findListPage(pageParams,"","");
        if(xhbWatchIPage!=null && xhbWatchIPage.getRecords().size()>0){
            return ResultBody.ok().data("已存在教案");
        }else {
            bhyfXhbWatch = bhyfWatchService.addBhyfXhbWatch(bhyfXhbWatch);
            return ResultBody.ok().data(bhyfXhbWatch);
        }
    }

    /**
     * 编辑教案
     * @param id
     * @param watchinfo
     * @param watchstatus
     * @param endword
     * @param endstatus
     * @return
     */
    @ApiOperation(value = "编辑教案", notes = "编辑教案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchinfo", value = "关注问题", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchstatus", value = "教案状态", required = false, paramType = "form"),
            @ApiImplicitParam(name = "endword", value = "改善总结", required = false, paramType = "form"),
            @ApiImplicitParam(name = "endstatus", value = "改善状态", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchend", value = "结束时间", required = false, paramType = "form")
    })
    @PostMapping("/watch/updateinfo")
    public ResultBody updateBhyfXhbWatch(
            @RequestParam(name = "id") Integer id,
            @RequestParam(name = "watchinfo",required = false) String watchinfo,
            @RequestParam(name = "watchstatus",required = false) Integer watchstatus,
            @RequestParam(name = "endword",required = false) String endword,
            @RequestParam(name = "endstatus",required = false) Integer endstatus,
            @RequestParam(name = "watchend",required = false) String watchend
    )
    {
        BhyfXhbWatch entiy = bhyfWatchService.findInfoById(id);
        if(watchinfo!=null){ entiy.setWatchinfo(watchinfo);}
        if(watchstatus!=null){entiy.setWatchstatus(watchstatus);}
        if(endword!=null){entiy.setEndword(endword);}
        if(endstatus!=null){entiy.setEndstatus(endstatus);}
        if(watchend!=null){entiy.setWatchend(DateUtil.parse(watchend));}
        entiy.setUpdatetime(new Date());
        bhyfWatchService.updateBhyfXhbWatch(entiy);
        return ResultBody.ok();
    }


    /**
     * 获取单体教案
     * @param id
     * @return
     */
    @ApiOperation(value = "获取单体教案", notes = "获取单体教案")
    @GetMapping("/watch/info")
    public ResultBody<BhyfXhbWatchInfo> getBhyfXhbWatchById(
            @RequestParam(required = true) Integer id
    )
    {
        BhyfXhbWatchInfo returnInfo = new BhyfXhbWatchInfo();
        BhyfXhbWatch watch = bhyfWatchService.findInfoById(id);
        returnInfo.setBhyfXhbWatch(watch);
        List<BhyfXhbWatchRecord> watchRecordList = bhyfWatchService.findRecordListByWatchId(id);
        returnInfo.setWatchRecordList(watchRecordList);
        return ResultBody.ok().data(returnInfo);
    }

    /**
     * 获取教案的单个观察记录信息
     * @param id
     * @return
     */
    @ApiOperation(value = "获取教案的单个观察记录信息", notes = "获取教案的单个观察记录信息")
    @GetMapping("/watch/recordinfo")
    public ResultBody<BhyfXhbWatchRecord> getBhyfXhbWatchRecordById(
            @RequestParam(required = true) Integer id
    )
    {
        BhyfXhbWatchRecord watchRecord = bhyfWatchService.findRecordInfoById(id);
        return ResultBody.ok().data(watchRecord);
    }

    /**
     * 新增教案的观察记录
     * @param personid
     * @param teacherid
     * @param teachername
     * @param watchid
     * @param recorddate
     * @param watchdate
     * @param watchresult
     * @param watchinfo
     * @param watchsuggest
     * @param watchpics
     * @return
     */
    @ApiOperation(value = "新增教案的观察记录", notes = "新增教案的观察记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personid", value = "学生ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "teacherid", value = "老师ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "teachername", value = "老师姓名", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchid", value = "教案ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "recorddate", value = "记录日期", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchdate", value = "观察日期", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchinfo", value = "观察问题", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchresult", value = "观察结果", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchsuggest", value = "观察意见", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchpics", value = "图片列表", required = false, paramType = "form")
    })
    @PostMapping("/watch/addrecordinfo")
    public ResultBody<BhyfXhbWatchRecord> addBhyfXhbWatchRecord
            (@RequestParam(name = "personid") Long personid,
             @RequestParam(name = "teacherid") Long teacherid,
             @RequestParam(name = "teachername") String teachername,
             @RequestParam(name = "watchid") Integer watchid,
             @RequestParam(name = "recorddate") String recorddate,
             @RequestParam(name = "watchdate") String watchdate,
             @RequestParam(name = "watchresult",required = false) String watchresult,
             @RequestParam(name = "watchinfo",required = false) String watchinfo,
             @RequestParam(name = "watchsuggest",required = false) String watchsuggest,
             @RequestParam(name = "watchpics",required = false) String watchpics
    )
    {
        BhyfXhbWatchRecord watchRecord = new BhyfXhbWatchRecord();
        watchRecord.setPersonid(personid);
        watchRecord.setTeacherid(teacherid);
        watchRecord.setTeachername(teachername);
        watchRecord.setRecorddate(DateUtil.parse(recorddate));
        watchRecord.setWatchdate(DateUtil.parse(watchdate));
        watchRecord.setWatchid(watchid);
        watchRecord.setWatchinfo(watchinfo);
        watchRecord.setWatchpics(watchpics);
        watchRecord.setWatchresult(watchresult);
        watchRecord.setWatchsuggest(watchsuggest);
        watchRecord.setCreatetime(new Date());
        watchRecord.setUpdatetime(new Date());
        watchRecord = bhyfWatchService.addBhyfXhbWatchRecord(watchRecord);
        return ResultBody.ok().data(watchRecord);
    }

    /**
     * 编辑教案的观察记录
     * @param id
     * @param watchresult
     * @param watchinfo
     * @param watchsuggest
     * @param watchpics
     * @return
     */
    @ApiOperation(value = "编辑教案的观察记录", notes = "编辑教案的观察记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "watchinfo", value = "观察问题", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchresult", value = "观察结果", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchsuggest", value = "观察意见", required = false, paramType = "form"),
            @ApiImplicitParam(name = "watchpics", value = "图片列表", required = false, paramType = "form")
    })
    @PostMapping("/watch/updaterecordinfo")
    public ResultBody updateBhyfXhbWatchRecord
            (
                    @RequestParam(name = "id") Integer id,
                    @RequestParam(name = "watchresult",required = false) String watchresult,
                    @RequestParam(name = "watchinfo",required = false) String watchinfo,
                    @RequestParam(name = "watchsuggest",required = false) String watchsuggest,
                    @RequestParam(name = "watchpics",required = false) String watchpics
            )
    {
        BhyfXhbWatchRecord watchRecord = bhyfWatchService.findRecordInfoById(id);
        if(watchRecord!=null) {
            if(watchinfo!=null){watchRecord.setWatchinfo(watchinfo);}
            if(watchpics!=null){watchRecord.setWatchpics(watchpics);}
            if(watchresult!=null){watchRecord.setWatchresult(watchresult);}
            if(watchsuggest!=null){watchRecord.setWatchsuggest(watchsuggest);}
            watchRecord.setUpdatetime(new Date());
            bhyfWatchService.updateBhyfXhbWatchRecord(watchRecord);
        }
        return ResultBody.ok();
    }

    /**
     * 删除教案的观察记录
     * @param id
     * @return
     */
    @ApiOperation(value = "删除教案的观察记录", notes = "删除教案的观察记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "form")
    })
    @PostMapping("/watch/deleterecordinfo")
    public ResultBody deleteBhyfXhbWatchRecord
            (
                    @RequestParam(name = "id") Integer id
            )
    {
        BhyfXhbWatchRecord watchRecord = bhyfWatchService.findRecordInfoById(id);
        if(watchRecord!=null) {
            watchRecord.setStatus(0);
            watchRecord.setUpdatetime(new Date());
            bhyfWatchService.updateBhyfXhbWatchRecord(watchRecord);
        }
        return ResultBody.ok();
    }
}
